Telegram Group & Telegram Channel
136. Binary search using recursion

#include<stdio.h>

int binary(int arr[], int n, int search, int l, int u);

int main()
{
int arr[10], i, n, search, c, l, u;

printf("Enter the size of an array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)
{
printf("Enter the element %d : ", i+1);
scanf("%d", &arr[i]);
}

printf("Enter the number to be search: ");
scanf("%d", &search);

l = 0, u = n - 1;
c = binary(arr, n, search, l, u);

if (c == 0)
printf("Number not found.");
else
printf("Number found.");

return 0;
}

int binary(int arr[], int n, int search, int l, int u)
{

int mid, c = 0;

if (l <= u)
{
mid = (l + u) / 2;
if (search == arr[mid])
{
c = 1;
}
else if (search < arr[mid])
{
return binary(arr, n, search, l, mid - 1);
}
else
return binary(arr, n, search, mid + 1, u);
}
else
return c;
}
@C_Codings



tg-me.com/C_Codings/198
Create:
Last Update:

136. Binary search using recursion

#include<stdio.h>

int binary(int arr[], int n, int search, int l, int u);

int main()
{
int arr[10], i, n, search, c, l, u;

printf("Enter the size of an array : ");
scanf("%d", &n);

for (i = 0; i < n; i++)
{
printf("Enter the element %d : ", i+1);
scanf("%d", &arr[i]);
}

printf("Enter the number to be search: ");
scanf("%d", &search);

l = 0, u = n - 1;
c = binary(arr, n, search, l, u);

if (c == 0)
printf("Number not found.");
else
printf("Number found.");

return 0;
}

int binary(int arr[], int n, int search, int l, int u)
{

int mid, c = 0;

if (l <= u)
{
mid = (l + u) / 2;
if (search == arr[mid])
{
c = 1;
}
else if (search < arr[mid])
{
return binary(arr, n, search, l, mid - 1);
}
else
return binary(arr, n, search, mid + 1, u);
}
else
return c;
}
@C_Codings

BY C Language πŸ‘¨β€πŸ’»


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/C_Codings/198

View MORE
Open in Telegram


C Language ‍ Telegram | DID YOU KNOW?

Date: |

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

C Language ‍ from jp


Telegram C Language πŸ‘¨β€πŸ’»
FROM USA